package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.ProjectMemberDao;
import com.hhs.storage.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-01-13
 */
@Service
@Transactional
public class ProjectMemberServiceImpl extends ServiceImpl<ProjectMemberDao, ProjectMember> implements ProjectMemberService {
    @Resource
    private BaseUserService baseUserService;
    @Resource
    BaseDepartmentService baseDepartmentService;
    @Resource
    BaseRoleService baseRoleService;
    @Resource
    ProjectPlanService projectPlanService;
    @Resource
    PersonalPlanService personalPlanService;
    @Resource
    private HttpServletRequest request;

    @Override
    public Map<String, Object> getMembersByProjectId(String projectId) {
        QueryWrapper<ProjectMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", projectId);
        List<ProjectMember> list = this.list(queryWrapper);
        for (ProjectMember projectMember : list) {
            BaseUser user = baseUserService.getById(projectMember.getUserId());
            projectMember.setUser(user);
            List<BaseRole> roleList = baseRoleService.getByUserId(user.getId());
            //获取每个用户的角色名称，方便前端展示
            if (roleList.size() > 0) {
                String temp = "";
                for (BaseRole o : roleList) {
                    temp += o.getName() + "，";
                }
                temp = temp.substring(0, temp.length() - 1);
                user.setRoleNames(temp);
            }
            //获取每个用户的部门名称，方便前端展示
            List<BaseDepartment> departList = baseDepartmentService.findByUserId(user.getId());
            if (departList.size() > 0) {
                String temp = "";
                for (BaseDepartment o : departList) {
                    temp += o.getName();
                    if (o.getType().equals(1)) {
                        temp += "负责人";
                    } else if (o.getType().equals(2)) {
                        temp += "职员";
                    }
                    temp += ",";
                }
                temp = temp.substring(0, temp.length() - 1);
                user.setDepartNames(temp);
            }
        }
        return ResponseInfo.success(list);
    }

    @Override
    public Map<String, Object> removeMember(String projectId, String memberId) {
        //只有经理，总经理，老板有权限新增编辑删除
//        String[] roles = new String[]{SysUtil.ROLE_FLAG_JL, SysUtil.ROLE_FLAG_ZJL, SysUtil.ROLE_FLAG_BOSS};
//        boolean flag = baseRoleService.hasRoles(roles, SysUtil.getLoginUserId(request));
//        if (flag == false) {
//            return ResponseInfo.error("当前账号无权限，必须是【经理，总经理，老板】角色账户");
//        }
        if (!projectPlanService.isProjectHeader(projectId)){
            return ResponseInfo.error("项目负责人才可删除成员");
        }
        ProjectPlan projectPlan = projectPlanService.getById(projectId);
        if (projectPlan.getState() == ProjectPlan.STATE_COMPLETE) {
            return ResponseInfo.error("已完成项目计划不可移除成员");
        }
//        if (projectPlan.getUserId().equals(memberId)) {
//            return ResponseInfo.error("项目创建者不可移除");
//        }
        //删除成员
        QueryWrapper<ProjectMember> qw = new QueryWrapper<>();
        qw.eq("project_id", projectId);
        qw.eq("user_id", memberId);
        this.remove(qw);
        //删除成员对应项目计划
        QueryWrapper<PersonalPlan> qw2 = new QueryWrapper<>();
        qw2.eq("project_id", projectId);
        qw2.eq("user_id", memberId);
        personalPlanService.remove(qw2);
        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> addMember(String projectId, String memberId) {
        //只有经理，总经理，老板有权限新增编辑删除
//        String[] roles = new String[]{SysUtil.ROLE_FLAG_JL, SysUtil.ROLE_FLAG_ZJL, SysUtil.ROLE_FLAG_BOSS};
//        boolean flag = baseRoleService.hasRoles(roles, SysUtil.getLoginUserId(request));
//        if (flag == false) {
//            return ResponseInfo.error("当前账号无权限，必须是【经理，总经理，老板】角色账户");
//        }
        if (!projectPlanService.isProjectHeader(projectId)){
            return ResponseInfo.error("项目负责人才可新增成员");
        }
        ProjectPlan projectPlan = projectPlanService.getById(projectId);
        if (projectPlan.getState() == ProjectPlan.STATE_COMPLETE) {
            return ResponseInfo.error("已完成项目计划不可添加成员");
        }
        QueryWrapper<ProjectMember> qw = new QueryWrapper<>();
        qw.eq("project_id", projectId);
        qw.eq("user_id", memberId);
        int total=this.count(qw);
        if (total>0){
            return ResponseInfo.error("成员已添加");
        }
        ProjectMember projectMember = new ProjectMember();
        projectMember.setUserId(memberId);
        projectMember.setProjectId(projectId);
        projectMember.setId(SysUtil.UUID());
        projectMember.setIdentityType(ProjectMember.IDENTITY_IDENTITY_TYPE_EXECUTOR);
        this.save(projectMember);
        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> changeIdentity(String id, Integer identityType) {
        //只有经理，总经理，老板有权限新增编辑删除
//        String[] roles = new String[]{SysUtil.ROLE_FLAG_JL, SysUtil.ROLE_FLAG_ZJL, SysUtil.ROLE_FLAG_BOSS};
//        boolean flag = baseRoleService.hasRoles(roles, SysUtil.getLoginUserId(request));
//        if (flag == false) {
//            return ResponseInfo.error("当前账号无权限，必须是【经理，总经理，老板】角色账户");
//        }

        ProjectMember projectMember = this.getById(id);
        if (projectMember == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (SysUtil.PROJECT_MEMBER_IDENTITY_TYPE_MAP.get(identityType) == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (!projectPlanService.isProjectHeader(projectMember.getProjectId())){
            return ResponseInfo.error("项目负责人才可设置项目角色");
        }
        //如果设置的是负责人身份，则原有负责人更改为执行人
        UpdateWrapper<ProjectMember> uw=new UpdateWrapper<>();
        uw.eq("project_id",projectMember.getProjectId());
        uw.eq("identity_type",ProjectMember.IDENTITY_IDENTITY_TYPE_HEADER);//查询负责人
        uw.set("identity_type",ProjectMember.IDENTITY_IDENTITY_TYPE_EXECUTOR);//执行人
        this.update(uw);
        projectMember.setIdentityType(identityType);
        this.updateById(projectMember);
        return ResponseInfo.success(projectMember);//返回前端使用
    }
}
